package top.jlzhou.tools.office.excel;

import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * <pre>通用导出Excel工具类</pre>
 * @ClassName	top.jlzhou.tools.office.excel.Export
 * @author	J.L.Zhou
 * @date	2019年3月9日 下午6:57:02
 * @version 	V1.0
 * @Copyright	2019 jlzhou.top Inc. All rights reserved. 
 * 	注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目
 */
public class Export {

	@SuppressWarnings("resource")
	public static void toExcel(List<?> list,String[] javas,String[] names,OutputStream out)throws Exception{
		InputStream in = Export.class.getResourceAsStream("export.xls");
		HSSFWorkbook workbook = new HSSFWorkbook(in);
		HSSFSheet sheet = workbook.getSheetAt(0);
		HSSFCellStyle style = sheet.getRow(0).getCell(0).getCellStyle();
		HSSFRow row = getRow(sheet, 0);
		for(int i=0;i<names.length;i++){
			HSSFCell cell = getCell(row, i);
			cell.setCellStyle(style);
			cell.setCellValue(names[i]);
		}
		if(list.size()>0){
			Class<?> cls = list.get(0).getClass();
			Method[] getters = new Method[javas.length];
			for(int i=0;i<javas.length;i++){
				getters[i] = cls.getMethod("get"+javas[i].substring(0, 1).toUpperCase()+javas[i].substring(1));
			}
			
			for(int i=0;i<list.size();i++){
				row = getRow(sheet, i+1);
				for(int j=0;j<getters.length;j++){
					HSSFCell cell = getCell(row, j);
					cell.setCellStyle(style);
					Object v = getters[j].invoke(list.get(i));
					if(v==null){
						cell.setCellValue("");
					}else{
						if(getters[j].getReturnType()==Date.class){
							String formatStr = "yyyy-MM-dd";
							if(getters[j].isAnnotationPresent(JSONField.class)){
								JSONField field = getters[j].getAnnotation(JSONField.class);
								if(!"".equals(field.format())){
									formatStr = field.format();
								}
							}
							Date d = (Date)v;
							SimpleDateFormat format = new SimpleDateFormat(formatStr);
							cell.setCellValue(format.format(d));
						}else{
							cell.setCellValue(v.toString());
						}
					}
				}
			}
		}
		workbook.write(out);
		
	}
	
	private static HSSFRow getRow(HSSFSheet sheet,int rowIndex){
		HSSFRow row = null;
		try{
			row = sheet.getRow(rowIndex);
		}catch(Exception ex){
			row = sheet.createRow(rowIndex);
		}
		if(row == null){
			row = sheet.createRow(rowIndex);
		}
		return row;
	}
	
	private static HSSFCell getCell(HSSFRow row,int cellIndex){
		HSSFCell cell = null;
		try{
			cell = row.getCell(cellIndex);
		}catch(Exception ex){
			cell = row.createCell(cellIndex);
		}
		if(cell==null){
			cell = row.createCell(cellIndex);
		}
		return cell;
	}
	
//	public static void main(String[] args)throws Exception {
//		List<Object> list = new ArrayList<Object>();
//		CbsBormBase base = new CbsBormBase();
//		base.setAccNo("test1");
//		base.setAcctNo("测试二");
//		list.add(base);
//		String[] javas = new String[]{"accNo","acctNo"};
//		String[] names = new String[]{"名称一","名称二"};
//		
//		OutputStream out = new FileOutputStream("d:\\upload\\aa.xls");
//		toExcel(list, javas, names, out);
		
//	}
}
